package com.andriology.ui;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.andriology.ommar_el_ard.R;


import com.actionbarsherlock.app.SherlockFragment;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Tracker;

public class AllVideos extends SherlockFragment{

	Tracker myTracker;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View view = inflater.inflate(R.layout.activity_all_videos, container, false);
		
		GridView videosGridView = (GridView) view.findViewById(R.id.videos_grid_view_id);
		videosGridView.setAdapter(new ImageAdapter(/*getSherlockActivity()*/));
		videosGridView.setOnItemClickListener(new OnItemClickListener() {
		String []youtubeVideoIds = getResources().getStringArray(R.array.youtube_video_ids);

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
				/*Toast.makeText(getSherlockActivity(), "Position "+position, Toast.LENGTH_SHORT).show();*/
			
				/* Google_Analytics*/
				myTracker.setCustomDimension(1, (getResources().getStringArray(R.array.test_names_array))[position]);
				myTracker.sendEvent("ui_action", "img_view_press", "video_img_view", 0L);
				
				/* send broadcast message to play the video */
				final String youTubeVideoWebPath = getResources().getString(R.string.video_url_prefix)+
														youtubeVideoIds[position];

				Intent intent= new Intent(Intent.ACTION_VIEW, Uri.parse(youTubeVideoWebPath));
				startActivity(intent);

			}
			
		});
		
		return view;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/* Set context */
		EasyTracker.getInstance().setContext(this.getSherlockActivity().getApplicationContext());
		
		/* Instantiate the Tracker */
		myTracker = EasyTracker.getTracker();
	}
    
	@Override
	public void onStart() {
		super.onStart();

		myTracker.sendView("AllVideos Screen");
	}
	
	public class ImageAdapter extends BaseAdapter {
	    /*private Context mContext;*/

	    public ImageAdapter(/*Context c*/) {
	    	/*mContext = c ;*/
	    }

	    public int getCount() {
	        return mThumbIds.length;
	    }

	    /*  should return the actual object at the specified position in the adapter, but it's ignored for this example*/
	    public Object getItem(int position) {
	        return null;
	    }

	    /*  should return the row id of the item, but it's ignored for this example*/
	    public long getItemId(int position) {
	        return -1;
	    }

	    /* create a new ImageView for each item referenced by the Adapter */
	    public View getView(int position, View convertView, ViewGroup parent) {
	    	
	    	ImageView imgViewThumbnail;
	    	TextView  videoTitle;
	    	LayoutInflater inflater = getLayoutInflater(null);
	    	
	        if (convertView == null) {  // if it's not recycled, initialize some attributes
	        	
	        	convertView =  inflater.inflate(R.layout.grid_view_item, parent, false);
	        	convertView.setLayoutParams(new GridView.LayoutParams(
	        			GridView.LayoutParams.WRAP_CONTENT, 
	        			250)
	        	);

	        	imgViewThumbnail = (ImageView) convertView.findViewById(R.id.img_view_thumbnail);
	        	videoTitle		 = (TextView)  convertView.findViewById(R.id.video_title_txt);

	        	/* old_part*/
	        	/*imgViewThumbnail = new ImageView(mContext);
	        	GridView.LayoutParams params = new GridView.LayoutParams(
	        			GridView.LayoutParams.WRAP_CONTENT, 
	        			250);
	        	
	        	imgViewThumbnail.setLayoutParams(new GridView.LayoutParams(params));*/
	        	/*imgViewThumbnail.setLayoutParams(new GridView.LayoutParams(300, 250));*/
	        	/*imgViewThumbnail.setScaleType(ScaleType.CENTER_CROP);*/

	        } else {
	        	imgViewThumbnail = (ImageView) convertView.findViewById(R.id.img_view_thumbnail);
	        	videoTitle		 = (TextView)  convertView.findViewById(R.id.video_title_txt);
	        	/*old_part*/
	        	/*imgViewThumbnail = (ImageView) convertView;*/
	        }

			
	        imgViewThumbnail.setImageResource(mThumbIds[position]);
	        videoTitle.setText((getResources().getStringArray(R.array.test_names_array))[position]);
	        return convertView;
	    }

	    /*references to our images*/
	    private Integer[] mThumbIds = {
	            R.drawable.ic_img_episode_01, R.drawable.ic_img_episode_02,
	            R.drawable.ic_img_episode_03, R.drawable.ic_img_episode_04,
	            R.drawable.ic_img_episode_05, R.drawable.ic_img_episode_06,
	            R.drawable.ic_img_episode_07, R.drawable.ic_img_episode_08,
	            R.drawable.ic_img_episode_09, R.drawable.ic_img_episode_10,
	            R.drawable.ic_img_episode_11, R.drawable.ic_img_episode_12,
	            R.drawable.ic_img_episode_13, R.drawable.ic_img_episode_14,
	            R.drawable.ic_img_episode_15, R.drawable.ic_img_episode_16,
	            R.drawable.ic_img_episode_17, R.drawable.ic_img_episode_18,
	            R.drawable.ic_img_episode_19
	    };

	}
	

}
